package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;


    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "dish",allEntries = true) //删除以dish开头的所有键值对
    public R<String> save(@RequestBody DishDto dishDto) {

        //保存菜品,菜品口味数据
        dishService.saveDishWithFlavor(dishDto);
        return R.success("添加菜品成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {

        //传统的分页查询无法满足数据显示,因为要分类名称
        Page<DishDto> pageInfo = dishService.pageDsihDto(page, pageSize, name);

        return R.success(pageInfo);
    }

    /**
     * 根据id批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "dish",allEntries = true) //删除以dish开头的所有键值对
    public R<String> deleteByIds(Long[] ids) {


        //批量删除,需要自己定义业务逻辑(菜品状态为启售不能删除)
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 启售,禁售状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        //批量修改状态
        //update dish set status=? where id in (ids)
        dishService.lambdaUpdate()
                //修改值
                .set(Dish::getStatus, status)
                //条件部分
                .in(Dish::getId, ids)
                //更新操作
                .update();
        return R.success("修改成功");
    }

    /**
     * 根据id查询菜品详细信息(包含多个口味数据)
     * 确定:返回一个对象, DishDto= Dish + List<DishFlavor>
     *
     * @return
     */
    @GetMapping("{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        DishDto dishDto = dishService.getDishByIdWithFlavor(id);
        return R.success(dishDto);

    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {


        //保存菜品,菜品口味数据
        dishService.updateDishByIdWithFlavor(dishDto);
        return R.success("修改成功");
    }

    @Autowired
    private CacheManager cacheManager;

    /**
     * 根据条件(菜品名称,分类id)查询菜品列表
     *
     * @param categoryId
     * @param name
     * @return
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "dish", key = "#categoryId +'_'+ #name")
    public R<List<DishDto>> list(Long categoryId, String name) {
        //查询数据库 select * from dish where category_id=? or name like?
//        List<Dish> list = dishService.lambdaQuery()
//                .eq(Dish::getStatus,1)
//                .eq(categoryId!=null,Dish::getCategoryId, categoryId)
//                .like(StringUtils.isNotBlank(name),Dish::getName, name)
//                .list();

        //说明缓存中没有查询到,需要查询数据库,注意:需要将数据存入redis,下次就能查询到
        List<DishDto> dishDtoList = dishService.listDishDto(categoryId, name);

        //返回数据
        return R.success(dishDtoList);
    }

}
